<template>
    <Header></Header>
    <Banner :isIndex="false" @changeCourse="changeCourse" :bannerList="banner_list" :courseList="course_list">
        <template #search>
            <el-button round plain type="primary" @click="toSearch" :icon="Search">搜索</el-button>
        </template>
    </Banner>
    <div class="question-body">
        <div class="breadcrumb">
            <el-breadcrumb :separator-icon="'ArrowRightBold'">
                <el-breadcrumb-item class="link" @click="$router.push({name:'index'})">首页</el-breadcrumb-item>
                <el-breadcrumb-item>{{activeName}}</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <el-row :gutter="20" v-if="SubjectList && SubjectList.data && SubjectList.data.length">
            <el-col
                    v-for="(item, index) in SubjectList.data"
                    :key="item.id"
                    :span="8"
                    :offset="index > 0 ? item.length : 0"
            >
                <el-card @click="continueStudy(item)" :body-style="{ padding: '0px',cursor:'pointer' }">
                    <img
                            :src="item.bannerPic"
                            class="image"
                    />
                    <div style="padding: 14px;display:flex;justify-content: space-between">
                        <span>{{item.name}}</span>
                        <span>章节数量：{{item.chapterNum}}</span>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <el-row v-else>
            <el-col>
                <el-empty description="暂无课程" />
            </el-col>
        </el-row>
    </div>
    <staticFooter></staticFooter>
</template>

<script>
    import staticFooter from "../../../components/staticFooter";
    import {Search} from '@element-plus/icons-vue'
    import freeVideo from "./tabs/freeVideo";
    import Banner from "../../../components/Banner";
    import Header from "@/components/Header";
    import {useRoute, useRouter} from "vue-router";
    import {bannerList, courseList, videoMyCoursesList} from "../../../api/api";
    import {reactive,ref,onMounted } from 'vue';
    import {ElMessage} from "element-plus";
    export default {
        name: "index",
        setup(){
            let router = useRouter();
            let route = useRoute();

            //当前课程
            let activeSubjectId = ref(0)
            let activeSubjectName = ref('')
            let activeName = ref('')
            activeName.value = route.params.activeName;
            activeSubjectId.value = localStorage.getItem('activeCourse')
            activeSubjectName.value = localStorage.getItem('activeCourseName')

            //banner图集合
            let banner_list = ref([])
            let getBannerList = () =>{
                bannerList({id:window.localStorage.getItem('activeCourse'),type:1}).then(res =>{
                    console.log('轮播图列表',res)
                    banner_list.value = res.data
                })
            }
            getBannerList();

            //科目列表
            let SubjectList = reactive({
                data:[]
            })
            let getSubjectList = (activeCourseID) =>{
                videoMyCoursesList({courseID:activeCourseID}).then(res =>{
                    SubjectList.data = res.data;
                    console.log('科目列表',SubjectList)
                }).catch(err =>{
                    ElMessage({type:'error',message:err.message})
                })
            }
            getSubjectList(localStorage.getItem('activeCourse'))

            //切换课程
            let changeCourse = (activeCourse) => {
                let activeCourseName = course_list.filter(item => item.courseID == activeCourse.courseId)[0].name;
                console.log('activeCourseName',activeCourseName)
                window.localStorage.setItem('activeCourseName', String(activeCourseName));
                activeSubjectId.value = localStorage.getItem('activeCourse')
                activeSubjectName.value = localStorage.getItem('activeCourseName')
                getBannerList();
                getSubjectList(activeCourse.courseId)
            }

            //课程列表
            let course_list = reactive([]);
            const getCourseList = () =>{
                courseList().then(res =>{
                    console.log('课程列表',res)
                    course_list.push(...res.data)
                    let acoutCourse = window.localStorage.getItem('activeCourse') ? window.localStorage.getItem('activeCourse'):course_list[0].courseID
                    window.localStorage.setItem('activeCourse',acoutCourse);
                })
            }
            getCourseList();

            let toSearch = () =>{
                router.push({name:'Search',params:{activeName:'course'}})
            }
            let continueStudy = (item) =>{
                console.log(item)
                router.push({name:'VideoPlayer',params:{subject_id:localStorage.getItem('activeCourse'),subject_name:localStorage.getItem('activeCourseName'),id:item.id,chapterID:0}})
            }
            return{
                continueStudy,
                SubjectList,
                toSearch,
                Search,
                changeCourse,
                activeName,
                activeSubjectId,
                activeSubjectName,
                course_list,
                banner_list,
            }
        },
        components:{
            Header,
            Banner,
            freeVideo,
            staticFooter
        }
    }
</script>

<style>
    .el-menu-item.is-active{
        background-color: rgba(42, 130, 228, 1);
    }
</style>

<style scoped>
    .image{
        width: 100%;
    }
    .link{
        cursor: pointer;
    }
    .link :hover{
        color: #409eff;
    }
    .question-body{
        width: 1200px;
        margin: 0 auto;
        margin-bottom: 240px;
    }
    .question-body .breadcrumb{
        margin: 20px 0px;
    }
</style>
